//---------------------------------------------------------------------------
// Copyright (c) 2010 Texas Instruments, Inc.
// This is an unpublished work created in the year stated above.
// Texas Instruments owns all rights in and to this work and
// intends to maintain and protect it as an unpublished copyright.
// In the event of either inadvertent or deliberate publication,
// the above stated date shall be treated as the year of first
// publication. In the evxfent of such publication, Texas Instruments
// intends to enforce its rights in the work under the copyright
// laws as a published work.
//----------------------------------------------------------------------------
// Filename                : afe_data.c
//
// Functional Description  : AFE register data declarations
//----------------------------------------------------------------------------

#include "DRV922XX.h"
#include "afe_data.h"

// Data (RAM)

unsigned char AfeRegGpio1Gp;
unsigned char AfeRegMisc3;
unsigned char AfeRegIcomp;
unsigned char AfeRegHallSns;
unsigned char AfeRegDac1;
unsigned char AfeRegDac2;
unsigned char AfeRegGpio2Gp;

// Saved data (FRAM)

const unsigned char AfeRegGpio1GpSaved = DEFAULT_AFE_REG_GPIO1GP;
const unsigned char AfeRegGpio2GpSaved = DEFAULT_AFE_REG_GPIO2GP;
const unsigned char AfeRegIcompSaved = DEFAULT_AFE_REG_ICOMP;
const unsigned char AfeRegHallSnsSaved = DEFAULT_AFE_REG_HALLSNS;
const unsigned char AfeRegMisc3Saved = DEFAULT_AFE_REG_MISC3;
const unsigned char AfeRegDac1Saved = DEFAULT_AFE_REG_DAC1;
const unsigned char AfeRegDac2Saved = DEFAULT_AFE_REG_DAC2;

/*
 *  \fn void afe_data_init()
 *  \brief Initializes the AFE data
 */
void afe_data_init()
{
	AfeRegGpio1Gp = AfeRegGpio1GpSaved;
	AfeRegGpio2Gp = AfeRegGpio2GpSaved;
	AfeRegIcomp = AfeRegIcompSaved;
	AfeRegHallSns = AfeRegHallSnsSaved;
	AfeRegMisc3 = AfeRegMisc3Saved;
	AfeRegDac1 = AfeRegDac1Saved;
	AfeRegDac2 = AfeRegDac2Saved;
}

/*
 * \fn void afe_data_save_parameters()
 * \brief Save AFE data parameters to FRAM
 */
void afe_data_save_parameters()
{
	*((unsigned int *)&AfeRegGpio1GpSaved) = AfeRegGpio1Gp;
	*((unsigned int *)&AfeRegGpio2GpSaved) = AfeRegGpio2Gp;
	*((unsigned int *)&AfeRegIcompSaved) = AfeRegIcomp;
	*((unsigned int *)&AfeRegHallSnsSaved) = AfeRegHallSns;
	*((unsigned int *)&AfeRegMisc3Saved) = AfeRegMisc3;
	*((unsigned int *)&AfeRegDac1Saved) = AfeRegDac1;
	*((unsigned int *)&AfeRegDac2Saved) = AfeRegDac2;
	
}

/*
 * \fn void afe_data_reset_defaults()
 * \brief Restore factory defaults.
 */
void afe_data_reset_defaults()
{
	*((unsigned int *)&AfeRegGpio1GpSaved) = DEFAULT_AFE_REG_GPIO1GP;
	*((unsigned int *)&AfeRegGpio2GpSaved) = DEFAULT_AFE_REG_GPIO2GP;
	*((unsigned int *)&AfeRegIcompSaved) = DEFAULT_AFE_REG_ICOMP;
	*((unsigned int *)&AfeRegHallSnsSaved) = DEFAULT_AFE_REG_HALLSNS;
	*((unsigned int *)&AfeRegMisc3Saved) = DEFAULT_AFE_REG_MISC3;
	*((unsigned int *)&AfeRegDac1Saved) = DEFAULT_AFE_REG_DAC1;
	*((unsigned int *)&AfeRegDac2Saved) = DEFAULT_AFE_REG_DAC2;
	
}
/*
 * \fn void afe_data_write_all()
 * \brief Write all non-zero AFE data to registers
 */
void afe_data_write_all()
{
	drv922xxSpiWrite(SPI_GPIO1GP_WRITE, AfeRegGpio1Gp);
	drv922xxSpiWrite(SPI_GPIO2GP_WRITE, AfeRegGpio2Gp);
	drv922xxSpiWrite(SPI_ICOMP_WRITE, AfeRegIcomp);
	drv922xxSpiWrite(SPI_HALLSNS_WRITE, AfeRegHallSns);
    drv922xxSpiWrite(SPI_MISC3_WRITE, AfeRegMisc3);
	drv922xxSpiWrite(SPI_DAC1_WRITE, AfeRegDac1);
	drv922xxSpiWrite(SPI_DAC2_WRITE, AfeRegDac2);
	
}

